/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JavaClass;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import generate.lessonList.LessonLists;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

/**
 *
 * @author USERT
 */
public class JaxBFunction {

    public static void XJCGenerateJavaObj(String output, String packagename, String schemalocation){
        try {
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                     System.out.println("error" + saxpe.getMessage());
                }

                public void fatalError(SAXParseException saxpe) {
                    System.out.println("error" + saxpe.getMessage());
                }

                public void warning(SAXParseException saxpe) {
                    System.out.println("error" + saxpe.getMessage());
                }

                public void info(SAXParseException saxpe) {
                    System.out.println("error" + saxpe.getMessage());
                }
            });
            sc.forcePackageName(packagename);
            File schema = new File(schemalocation);
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (Exception ex){
            ex.printStackTrace();
        }

    }

     public static void marshall(String filepath, Object obj) {
        try {
            JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(obj, new File(filepath));
        } catch (JAXBException ex) {
            ex.printStackTrace();
        }
    }
}
